package com.snow.datasource.provider;

import com.snow.datasource.creator.IDataSourceCreator;
import com.snow.datasource.properties.DataSourceProperty;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import javax.sql.DataSource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 数据源加载模板类
 *
 * @Description
 * @Author fanxl 315620726@qq.com
 * @Date 2020/12/16
 **/
public abstract class AbstractSnowProvider implements ISnowProvider {
    @Autowired
    private IDataSourceCreator iDataSourceCreator;

    /**
     * 装载数据源
     *
     * @return
     */
    public Map<String, DataSource> createDataSources(Map<String, DataSourceProperty> dataSourcePropertiesMap) {
        Map<String, DataSource> createDataSourceMap = new ConcurrentHashMap<>(16);
        for (String key : dataSourcePropertiesMap.keySet()) {
            DataSourceProperty dataSourceProperty = dataSourcePropertiesMap.get(key);
            DataSource dataSource = iDataSourceCreator.createDataSource(dataSourceProperty);
            createDataSourceMap.put(key, dataSource);
        }
        return createDataSourceMap;
    }
}
